package com.sfac.javaSpringBoot.modules.test.controller;

import org.springframework.data.domain.Page;
import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.common.entity.SearchBean;
import com.sfac.javaSpringBoot.modules.test.entity.Student;
import com.sfac.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /*
    * 127.0.0.1/api/student   ---- post
    * {"name":"hujiang","email":"hj@163.com","age":18}
    * {"name":"hujiang","email1":"hj1@163.com","age":19,"studentCard":{"cardNo":"SFAC_00001"}}
    * */
    @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/student   ---- put
     * {"name":"hujiang","email":"hj@163.com","age":18}
     *
     */
    @PutMapping(value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }
    /*
    * 127.0.0.1/api/student/id?   -----delete
    * */
    @DeleteMapping("/student/{id}")
    public Result<Object> deleteStudentById(@PathVariable int id) {
        return studentService.deleteStudentById(id);
    }

    /*
    * 127.0.0.1/api/student/id?  ---get
    * */
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }

    /*
    * 127.0.0.1/api/students   ---get
    * */
    @GetMapping("/students")
    public List<Student> getStudents() {
        return studentService.getStudents();
    }

    /*
    * 127.0.0.1/api/student?name=hujiang1&email=hj1@163.com  ---get
    * */
    @GetMapping("/student")
    public Student getStudentByNameAndEmail(@RequestParam String name,
                                            @RequestParam String email) {
        return studentService.getStudentByNameAndEmail(name, email);
    }

    /*
    * 127.0.0.1/api/students ----post
    *  {"currentPage":1,"pageSize":5,"orderBy":"name","direction":"desc","keyword":""}
    * */
    @PostMapping(value = "/students", consumes = "application/json")
    public Page<Student> getStudentsBySearchBean(@RequestBody SearchBean searchBean) {
        return studentService.getStudentsBySearchBean(searchBean);
    }
}
